package util;

import java.util.Vector;

import wdmsim.OXC;
import wdmsim.WDMLink;

public class Auxiliar {

	/**
	 * 
	 * @param A
	 * @param B
	 * @return
	 */
	public static int[] concat(int[] A, int[] B) {
		int[] C = new int[A.length+B.length-1];
		System.arraycopy(A, 0, C, 0, A.length-1);
		System.arraycopy(B, 0, C, A.length, B.length);

		return C;
	}
	
	/**
	 * 
	 * @param A
	 * @param B
	 * @return
	 */
	public static int[] concat2(int[] A, int[] B) {
		int[] C = new int[A.length+B.length];
		System.arraycopy(A, 0, C, 0, A.length);
		System.arraycopy(B, 0, C, A.length, B.length);

		return C;
	}
	
	/**
	 * returns if array constains x
	 */
	public static boolean contains(int[] array, int x) {
		for(int i=0; i<array.length; i++) {
			if(array[i]==x)
				return true;
		}
		return false;
	}
	
	/**
	 * returns if array constains x
	 */
	public static boolean contains(OXC[] array, OXC x) {
		for(int i=0; i<array.length; i++) {
			if(array[i]==x)
				return true;
		}
		return false;
	}

	/**
	 * returns if array contains x AND x is not the tail of array 
	 */
	public static boolean crosses(int[] array, int x) {
		for(int i=0; i<array.length-1; i++) {
			if(array[i]==x)
				return true;
		}
		return false;
	}
	
	/**
	 * returns if a is equal to b (has the same values on it)
	 * @param a
	 * @param b
	 * @return
	 */
	public static boolean arrayEqual(int[] a, int[] b) {
		if(a.length==b.length) {
			for(int i=0; i<a.length; i++) {
				if(a[i]!=b[i])
					return false;
			}
			
		} else {
			return false;
		}
		return true;
	}

	/**
	 * @param bool
	 * @param freeWavelengths
	 * @return
	 */
	public static boolean[] booleanIntersection(boolean[] b1, boolean[] b2) {
		int k = 0;
		boolean r[] = new boolean[Math.min(b1.length, b2.length)];
		
		if(b1.length<b2.length) {			
			for(int i=0; i<b1.length; i++) {
				for(int j=0; j<b2.length; j++) {
					if(b1[i]==b2[j]) {
						r[k++] = b1[i];
						break;
					}
				}
			}
			
		} else {
			for(int i=0; i<b2.length; i++) {
				for(int j=0; j<b1.length; j++) {
					if(b2[i]==b1[j]) {
						r[k++] = b2[i];
						break;
					}
				}
			}
		}
		return null;
	}
	
	/**
	 * @param bool
	 * @param freeWavelengths
	 * @return
	 */
	public static int[] intIntersection(int[] int1, int[] int2) {
		int k = 0;
		int r[] = new int[Math.min(int1.length, int2.length)];
		for(int i=0; i<r.length; i++) {
			r[i] = -1;
		}
		
		if(int1.length<int2.length) {
			for(int i=0; i<int1.length; i++) {
				for(int j=0; j<int2.length; j++) {
					if(int1[i]==int2[j]) {
						r[k++] = int1[i];
						break;
					}
				}
			}
			
		} else {
			for(int i=0; i<int2.length; i++) {
				for(int j=0; j<int1.length; j++) {
					if(int2[i]==int1[j]) {
						r[k++] = int2[i];
						break;
					}
				}
			}
		}
		return adjustIntArray(r);
	}
	
	/**
	 * retira os valores '-1' do fim de array.
	 * esse metodo eh privado, e somente chamado pelo metodo intIntersection()
	 * 
	 * @param array
	 * @return
	 */
	private static int[] adjustIntArray(int[] array) {
		if(array.length>0) {
			int cont = 0;
			for(int i=0; i<array.length; i++) {
				if(array[i]!=-1) {
				//if(array[i]!=0) {
					cont++;
				}
			}
			int r[] = new int[cont];
			for(int i=0; i<cont; i++) {
				r[i] = array[i];
			}
			return r;
			
		} else {
			return array;
		}		
	}

	/**
	 * retorna o indice do primeiro valor diferente entre domainPath e dp
	 * 
	 * @param domainPath
	 * @param dp
	 */
	public static int findWrongDomainNode(int[] domainPath, int[] dp) {
		if(domainPath.length>dp.length) {
			for(int i=0; i<domainPath.length && i<dp.length; i++) {
				if(domainPath[i]!=dp[i]) {
					return i;
				}
			}
			
		} else {
			for(int i=0; i<dp.length && i<domainPath.length; i++) {
				if(domainPath[i]!=dp[i]) {
					return i;
				}
			}
		}
		return -1;
	}

	public static Vector<Integer> array2Vector(int[] pathFreeWavelengths) {
		Vector<Integer> vector = new Vector<Integer>();
		for(int i=0; i<pathFreeWavelengths.length; i++) {
			vector.add((Integer)pathFreeWavelengths[i]);
		}
		return vector;
	}
	
	public static int[] vector2Array(Vector<Integer> vector) {
		int[] array = new int[vector.size()];
		for(int i=0; i<vector.size(); i++) {
			array[i] = vector.get(i);
		}
		return array;
	}
	
	/*public static WDMLink[] vector2Array(Vector<WDMLink> vector) {
		WDMLink[] array = new WDMLink[vector.size()];
		for(int i=0; i<vector.size(); i++) {
			array[i] = vector.get(i);
		}
		return array;
	}*/

	public static boolean hasSequence(int[] path, int srcId, int dstId) {
		for(int i=0; i<path.length-1; i++) {
			if(path[i]==srcId) {
				if(path[i+1]==dstId) {
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * returns the index of the first ocurrence of domainId
	 * @param domainPath
	 * @param domainId
	 * @return
	 */
	public static int arrayIndex(int[] domainPath, int domainId) {
		for(int i=0; i<domainPath.length; i++) {
			if(domainPath[i]==domainId) {
				return i;
			}
		}
		return -1;
	}
	
}
